package cn.wsalix.admin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.service.impl.ParentServiceImpl;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.utils.BeanMapper;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.form.RoleForm;
import cn.wsalix.admin.repository.RolePermitRepository;
import cn.wsalix.admin.repository.RoleRepository;
import cn.wsalix.admin.service.PermitService;
import cn.wsalix.admin.service.RoleService;

@Service("roleService")
public class RoleServiceImpl extends
		ParentServiceImpl<RoleForm, RoleRepository, SysRole> implements
		RoleService {
	@Autowired
	private RoleRepository roleRepository;

	@Autowired
	private RolePermitRepository rolPermitRepository;

	@Autowired
	private PermitService permitService;

	@Override
	public RoleRepository getRepository() {
		return roleRepository;
	}

	@Override
	public String module() {
		return "/admin/role";
	}
/*
	@Override
	public List<RolePermit> findPermitsByUserId(Long userId) {
		return rolPermitRepository.findPermsByUserId(userId);
	}
*/
	@Override
	public Class<SysRole> getEntityClazz() {
		return SysRole.class;
	}

	@Transactional
	@Override
	public void delete(Long id) {
		SysRole obj = getRepository().findOne(id);
		obj.setFlag(FlagEnum.delete);
		edit(obj);
	}
	@Transactional
	@Override
	public SysRole add(RoleForm form) {
		BaseUser user = userUtils.getUser();
		SysRole obj = BeanMapper.map(form, getEntityClazz());
		traversalAssign(form, obj);
		if (form.getParentId() != null) {
			SysRole parent = getRepository().findOne(form.getParentId());
			obj.setParent(parent);
			obj.setLevel(parent.getLevel() + 1);
		} else {
			obj.setLevel(1);
		}
		obj.setCreatedDate(new Date());// DateTime.now());
		if (user != null) {
			obj.setCreatedBy(user.getId());
		}
		obj.setFlag(FlagEnum.valid);
		return super.add(obj);
	}

	@Override
	public Map<String, SearchFilter> buildByForm(RoleForm form) {
		Map<String, SearchFilter> filters = buildFilter(form);
		if (form.getName() != null && !form.getName().equals("")) {
			filters.put("name", new SearchFilter("name", Operator.LIKE,
					form.getName()));
		}
		return filters;
	}

	@Override
	public List<SysRole> findUnRolesByMenuId(Long menuId) {
		return roleRepository.findUnRolesByMenuId(menuId);
	}

	@Override
	public SysRole findByCodeOne(String code) {
		logger.info(code);
		List<SysRole> objs = findsByCode(code);
		if (objs.size() != 1) {
			if (objs.size() > 1) {
				logger.error(code + " : size() > 1 [" + objs.size() + "]");
				return objs.get(0);
			}
			logger.error(code + " : size() =0 ");
			return null;
		} else {
			return objs.get(0);
		}
	}

	@Override
	public SysRole findByNameOne(String name) {
		logger.info(name);
		List<SysRole> resLst = findsByName(name);
		if (resLst.size() != 1) {
			if (resLst.size() > 1) {
				logger.error(name + " : size() > 1 [" + resLst.size() + "]");
				return resLst.get(0);
			}
			logger.error(name + " : size() =0 ");
			return null;
		} else {
			return resLst.get(0);
		}
	}

	@Override
	public List<SysRole> findsByName(String name) {
		return roleRepository.findByName(name);
	}

	@Override
	public List<SysRole> findsByCode(String code) {
		return roleRepository.findByCode(code);
	}

/*	@Override
	public void initRoles() {
		roleRepository.delByMaster("true");
	}*/

	@Override
	public List<SysRole> findUnRolesByUserId(Long userId) {
		return roleRepository.findUnRolesByUserId(userId);
	}

}
